/**
 * @author : xwj
 * @date : 2021/6/3 21:14
 * @description : LeetCode 第六题
 * @class : six
 *
 * 将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。
 **/

public class test6 {

    public static void main(String[] args) {
        String s = "PAYPALISHIRING";
        System.out.println(convert("AB",1));
    }

    public static String convert(String s, int numRows) {
        int i = s.length()/numRows + s.length()/2;
        int flag = 0,h = 0,l = 0;
        char[][] c = new char[numRows][i];
        for (int j = 0; j < numRows; j++) {
            for (int k = 0; k < i; k++) {
                c[j][k] = ' ';
            }
        }
        for (int m = 0; m < s.length(); m++){
            if (numRows == 1){
                return s;
            }
            if (h == 0){
                flag = 0;
            }
            if(h == numRows-1){
                flag = 1;
            }
            if (flag == 0){
                c[h][l] = s.charAt(m);
                h++;
            }else {
                c[h][l] = s.charAt(m);
                h--;
                l++;
            }
        }
        String s1 = "";
        for (int j = 0; j < numRows; j++) {
            for (int k = 0; k < i; k++) {
                if (c[j][k] != ' '){
                    s1 = s1 + c[j][k];
                }
            }
        }
        return s1;
    }

}
